package su.user.util;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import su.user.dao.IUserYwyDAO;
import su.user.entity.UserYwy;

/**
 * 业务员map，key为id,value为业务员姓名
 * @author Administrator
 *
 */
@Repository
public class YwyCache {

	@Autowired
	private IUserYwyDAO ywyDAO;
	
	private Map<Integer,String> ywyMap;
	
	private List<UserYwy> ywyList;
	
	public String getYwynameById(int id){
		if(getYwyMap()==null)return "";
		return ywyMap.get(id);
	}
	
	public Map<Integer,String> getYwyMap(){
		if(ywyMap==null){
			this.setYwyMap();
		}
		return ywyMap;
	}
	
	public void removeYwyMap(){
		ywyMap = null;
	}
	
	public List<UserYwy> getYwyList(){
		if(ywyList!=null)return ywyList;
		try {
			ywyList = ywyDAO.getAllYwy();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return ywyList;
	}
	
	private void setYwyMap(){
		ywyList = getYwyList();
		if(ywyList==null || ywyList.isEmpty())return;
		ywyMap = new HashMap<Integer, String>();
		for(UserYwy one : ywyList){
			ywyMap.put(one.getId(), one.getYwyname());
		}
	}
}
